/**
 * Created With IntelliJ IDEA
 * Description:leetcode：203. 移除链表元素
 * <a href="https://leetcode.cn/problems/remove-linked-list-elements/">...</a>
 * User: DELL
 * Data: 2022-12-05
 * Time: 15:42
 */

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}


public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        //判空处理
        if (head == null) {
            return null;
        }
        //处理除第一个结点外的结点
        ListNode pre = head;
        while (pre.next != null) {
            if (pre.next.val == val) {
                pre.next = pre.next.next;
            } else {
                pre = pre.next;
            }
        }
        //处理第一个结点
        if (head.val == val) {
            head = head.next;
        }
        return head;
    }
}